/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.server.virtual.model.sax;

import java.io.IOException;
import javax.jcr.RepositoryException;
import javax.servlet.ServletException;
import offset.nodes.server.html.model.HttpContext;
import javax.jcr.Session;
import offset.nodes.server.controller.AttributeConstants;
import offset.nodes.server.virtual.model.VirtualModel;

/**
 * The predefined java extension for transformation.
 * Must be initialized before the transformation with the HttpContext.
 * 
 * @author Walter L�tgenau
 */
public class TransformerUtils {

    /**
     * The ThreadLocal, that holds the thread specific context
     */
    static ThreadLocal<HttpContext> httpContext = new ThreadLocal<HttpContext>() {

        @Override
        protected HttpContext initialValue() {
            return null;
        }
    };
    static TransformerUtils instance;

    public static synchronized TransformerUtils getInstance() {
        if (instance == null)
            instance = new TransformerUtils();
        return instance;
    }

    /**
     * Initialization of the context for the thread just before the transformation.
     *
     * @param context the context
     */
    public void setContext(HttpContext context) {
        httpContext.set(context);
    }

    public HttpContext getContext() {
        HttpContext context = httpContext.get();
        return context;
    }

    public Session getSession() {
        if (getContext() == null)
            return null;

        return (Session) getContext().getRequest().getSession().getAttribute(AttributeConstants.ATT_REPOSITORY_SESSION);
    }

    /**
     * Initialize a VirtualMode, which will do the work.
     *
     * @return the model
     */
    protected VirtualModel getModel() {
        return new VirtualModel(getSession());
    }

    /**
     * Return the page content of the referenced node.
     * The page content is the HTML element with id "content" of the page referenced by the node UUID.
     *
     * @param pageReference the UUID of the page
     * @return the page content
     */
    public static String getPageContent(String pageReference) throws ServletException, IOException, Exception {
        if (getInstance().getContext() == null)
            return null;

        return getInstance().getModel().getPageContent(pageReference, getInstance().getContext());
    }
    
    /**
     * Return a repository hyperlink for the reference 
     * 
     * @param nodeReference
     * @return
     * @throws RepositoryException 
     */
    public static String getHyperlink(String nodeReference) throws RepositoryException {
        return getInstance().getModel().getRepositoryHyperlink(getInstance().getContext().getRequest(), nodeReference);
    }
}
